﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MyVoteMVC.Common.Objects
{
    public class EmailEncode
    {
        public const string SevenBit = "7Bit";
        public const string EightBit = "8Bit";
        public const string BASE64 = "base64";
    }

    public static class Formatter
    {
        public static string DateFormat(DateTime date)
        {
            return string.Format("Date:{0}", date.ToLocalTime().ToString("r"));
        }

        public static string SubjectFormat(string subject, Encoding encoding)
        {
            return string.Format("Subject:{0}", ConvertToBase64String(subject,encoding));
        }

        public static string BodyHeaderFormat(bool isHtml, Encoding encoding)
        {
            return string.Format("Content-Type:{0};charset=\"{1}\"", isHtml ? "text/html" : "text/plain", encoding.BodyName);
        }

        public static string ConvertToBase64String(string value, Encoding encoding)
        {
            byte[] subs = encoding.GetBytes(value);
            return string.Format("=?{0}?B?{1}?=", encoding.BodyName, Convert.ToBase64String(subs));
        }

        public static string PriorityFormat(System.Net.Mail.MailPriority priority)
        {
            int priority2 = 0;
            switch (priority)
            {
                case System.Net.Mail.MailPriority.High:
                    priority2 = 1;
                    break;
                case System.Net.Mail.MailPriority.Low:
                    priority2 = 5;
                    break;
                case System.Net.Mail.MailPriority.Normal:
                    priority2 = 3;
                    break;
                default:
                    priority2 = 3;
                    break;

            }
            return string.Format("X-Priority:{0}", priority2);
        }

        public static string EncodingFormat(Encoding encoding)
        {
            return string.Format("Content-Transfer-Encoding: {0}", encoding.BodyName);
        }

        public static string EncodingFormat(string encodeName)
        {
            return string.Format("Content-Transfer-Encoding: {0}", encodeName);
        }

        //定义分隔符
        public static string BoundaryFormat(string boundary)
        {
            return string.Format("Content-Type: multipart/mixed; boundary=\"{0}\"\r\n", boundary);
        }

        //设置开始分隔符
        public static string BoundaryBeginFormat(string boundary)
        {
            return string.Format("--{0}", boundary);
        }

        //设置结束分隔符
        public static string BoundaryEndFormat(string boundary)
        {
            return string.Format("--{0}--", boundary);
        }

        /// <summary>
        /// 发件人
        /// </summary>
        /// <param name="address"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string FormFormat(string address)
        {
            return string.Format("From:{0}", address);
        }

        public static string AddressFormat(string address, string displayName)
        {
            return string.Format("{0}<{1}>", displayName, address);
        }
        /// <summary>
        /// 收件人
        /// </summary>
        /// <param name="address"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string TosFormat(List<string> address)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < address.Count; i++)
            {
                sb.Append(address[i]);
                if (i != address.Count - 1)
                    sb.Append(",");
            }
            return string.Format("To:{0}", sb.ToString());
        }

        /// <summary>
        /// 邮件ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string MessageIdFormat(string id)
        {
            return string.Format("Message-Id:{0}", id);
        }

    }
}
